#include "Officer.h"
#include "BehaviorManager.h"
#include "BaseBehavior.h"
#include "../SGD Wrappers/SGD_GraphicsManager.h"
#include "GameplayState.h"
#include "Player.h"
#include "CreateBulletMessage.h"
#include "../SGD Wrappers/SGD_Event.h"


Officer::Officer() : Listener(this)
{

	RegisterForEvent("TARGET_FOUND");
	RegisterForEvent("ALARM_TRIGGER");
	RegisterForEvent("ALARM_OFF");
	RegisterForEvent("STUNNED");



	m_FieldOfView.SetOwner(this);
	animation.m_strCurrAnimation = "officer";
	animation.m_fCurrDuration = 0.0f;
	animation.m_nCurrFrame = 0;

}
Officer::~Officer()
{


	
}

/*virtual*/ void Officer::Update(float dt)	 /*override*/
{
	

	if (currBehavior != nullptr)
	{
		if (m_nAIState == PATROL)
		{
			if (currBehavior->Update(dt, this, *navGraph->GetVertexElement(m_nCurrWaypoint)))
			{
				
				m_nCurrWaypoint++;
				if (m_nCurrWaypoint == m_vWayPoints.size())
					m_nCurrWaypoint = 0;
	
			}
		}
		else if (m_nAIState == ALERT)
		{
			currBehavior->Update(dt, this, m_pTarget->GetPosition());

		}
		else if (m_nAIState == GO_HOME)
		{
			currBehavior->Update(dt, this, { 0, 0 });
			
		}
		
	}
		
	EnemyUnit::Update(dt);

}

/*virtual*/ void Officer::HandleEvent(const SGD::Event* pEvent)
{

	if ( pEvent->GetEventID() == "TARGET_FOUND"
		|| pEvent->GetEventID() == "ALARM_TRIGGER")
	{
		m_nAIState = GO_HOME;
		RetrieveBehavior("runTo");
	}

	if (pEvent->GetEventID() == "ALARM_OFF")
	{
		m_nAIState = PATROL;
		RetrieveBehavior("move");
	}
	else if (pEvent->GetEventID() == "STUNNED")
		m_tStunTimer.AddTime(5);
}
/*virtual*/ void Officer::HandleCollision(const IBase* pOther) /*override*/
{
	if (pOther->GetType() == OBJ_PLAYER)
	{
		m_nAIState = ALERT;
		RetrieveBehavior("hostile");

	}
	else if (pOther->GetType() == OBJ_TRIGGER || pOther->GetType() == OBJ_PLAYERCOLLISION)
	{

	}
	MovingObject::HandleCollision(pOther);
	
}


void Officer::RetrieveBehavior(std::string name)
{

	if (currBehavior != nullptr)
	{
		currBehavior = nullptr;
	}
	currBehavior = BehaviorManager::GetInstance()->GetBehaviors()[name];
}

/*virtual*/ void Officer::Attack() /*override*/
{
	m_tShotTimer.AddTime(1.0f);
	CreateBulletMessage* pMsg = new CreateBulletMessage(this);
	pMsg->QueueMessage();
	pMsg = nullptr;
}
